//
//  QDPayTypeStatusViewController.swift
//  QDMetroClient
//
//  Created by YueWen on 2019/3/9.
//  Copyright © 2019 YueWen. All rights reserved.
//

import UIKit

/// 轮寻获得签约状态的视图
final class QDPayTypeStatusViewController: NWTableViewController {

    private let timerName = "QDPayTypeStatusViewControllerSign"
    
    /// 签约协议的id
    var channelId: String = ""
    /// 支付方式
    var payType: QDPayType = .ALiPay
    /// 支付方式的header
    let headerView = QDPayTypeSettingHeaderView()
    /// 返回的控制器
    weak var backViewController: UIViewController?
    
    convenience init(channelId: String,payType: QDPayType = .ALiPay, backViewController: UIViewController? = nil){
        self.init()
        self.channelId = channelId
        self.payType = payType
        self.backViewController = backViewController
    }
    
    
    override func loadPropertysAtInitialization() {
        super.loadPropertysAtInitialization()
        
        headerRefreshEnable = false
        footerRefreshEnable = false
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        navigationItem.title = "签约"
        headerView.ritl_width = ritl_width
        headerView.ritl_height = QDPayTypeSettingHeaderView.height
        tableView.tableHeaderView = headerView
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(image: backIndicatorImage.namedImage, landscapeImagePhone: "nav_main_back".namedImage, style: .done, target: self, action: #selector(back))
        
        //设置头部属性
        headerView.imageView.image = payType.payTypeLargeImage.namedImage
        headerView.titleLabel.text = "\(payType.title)签约查询中..."
        headerView.subtitleLabel.text = "绑定\(payType.title)先乘后付"
        
        //启用轮播
        JX_GCDTimerManager.sharedInstance()?.scheduledDispatchTimer(withName: timerName, timeInterval: 3, queue: DispatchQueue.main, repeats: true, fireInstantly: false, action: {[weak self] in
            if let strongSelf = self {
                strongSelf.requestSignStatus()
            }
        })
    }
    
    // MARK: NavigationBar
    override var nav_backInteractive: Bool { //禁止手势
        return false
    }
    
    /// 返回
    @objc func back(){
        
        if let backViewController = self.backViewController {
            navigationController?.popToViewController(backViewController, animated: true); return
        }
        
        navigationController?.popViewController(animated:  true)
    }
    
    
    /// 轮寻签约状态
    @objc func requestSignStatus(){
        
        QDNetworkingManager.request(url: QDCustomerHostUrl + "/sign/agreementStatus", paramteres: ["id": channelId], success: { (msg, data) in
            
            if let status = data["status"] as? String, status == "13" {//表示签约成功
                print("签约成功")
                self.headerView.titleLabel.text = "\(self.payType.title)签约成功"
                JX_GCDTimerManager.sharedInstance()?.cancelTimer(withName: self.timerName)
                //发送全局通知
                NotificationCenter.default.post(name: QDPayTypeStatusDidChangedNotificationName, object: nil)
            }
            
            print("继续查询")
            
        }) { (msg, errorType) in
            self.view.showTextHUD(text: msg)
        }
    }
}
